// pages/purchasing/purchasing.js
var app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: '0',
    mycurrent: '待采购',
    list: [],
    page: 1,
    user: wx.getStorageSync('user'),
    imgApi: app.globalData.imgApi,
    fabricList: [], //面料
    auxiliaryFabricList: [], //辅料
    total:0.00,
    myList:[],
    ordName:'',
    items:'true'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.getList(1)
    this.setData({
      user: wx.getStorageSync('user')
    })
  },
  getList: function(page) {
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/list',
      data: {
        page: page,
        rows: 20
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        console.log(res)
        if (res.data.code == 200) {
          for (var i in res.data.data.designDiagramList) {
            that.data.list.push(res.data.data.designDiagramList[i])
            that.data.list[i].img = that.data.list[i].designImage.split(',')[0]
          }
          that.setData({
            list: that.data.list,
            rowsTotal: res.data.data.rowsTotal
          })
          that.data.page += 1
        }else{
          if (that.data.rowsTotal == '') {
            that.setData({
              list: []
            })
          }
        }
      }
    })
  },
  search: function(e) {
    var that = this
    wx.request({
      url: app.globalData.api + 'designDiagram/list',
      data: {
        page: 1,
        rows: 99,
        keyword: e.detail.value.key
      },
      method: 'POST',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function(res) {
        if (res.data.code == 200) {
          that.data.list = []
          for (var i in res.data.data.designDiagramList) {
            that.data.list.push(res.data.data.designDiagramList[i])
            that.data.list[i].img = that.data.list[i].designImage.split(',')[0]
          }
          that.setData({
            list: that.data.list
          })
        }else{
          that.setData({
            list: []
          })
        }
      }
    })
  },
  disSear: function(e) {
    if (e.detail.value == '') {
      this.data.list = []
      this.data.page = 1
      this.getList(1)
    }
  },
  goDet: function(e) {
    wx.navigateTo({
      url: '/pages/details/details?id=' + e.currentTarget.dataset.id
    })
  },
  tabbarChange(e) {
    this.setData({
      current: e.detail.key,
    })
    if (e.detail.key == 2) {
      this.getMyList(this.data.mycurrent)
    } else if (e.detail.key == 0){
      this.setData({
        list:[],
        page:1
      })
      this.getList(1)
    }
  },
  MyonChange(e) {
    console.log('onChange', e)
    this.setData({
      mycurrent: e.detail.key,
    })
    this.getMyList(e.detail.key)


  },
  addFabric: function(e) {
    wx.navigateTo({
      url: '/pages/addFabric/addFabric?type=' + e.currentTarget.dataset.type + '&i=false'
    })
  },
  add(e){
    wx.navigateTo({
      url: '/pages/purchasing_order/purchasing_order?id=' + e.currentTarget.dataset.id
    })
  },
  edit: function(e) {
    wx.navigateTo({
      url: '/pages/order_det/order_det?id=' + e.currentTarget.dataset.id
    })
  },
  deFabric: (e) => {
    wx.navigateTo({
      url: '/pages/deFabric/deFabric?id=' + e.target.dataset.id + '&index=' + e.target.dataset.index + '&type=' + e.target.dataset.type
    })
  },
  edilt:function(e){
    console.log(e)
    wx.navigateTo({
      url: '/pages/addFabric/addFabric?type=' + e.currentTarget.dataset.type + '&index=' + e.currentTarget.dataset.index + '&i=false'
    })
  },
  orName:function(e){
    this.setData({
      ordName:e.detail .value
    })
  },
  sub:function(){
    var that = this
    if (this.data.ordName==''){
      wx.showModal({
        title: '提示',
        content: '请填写订单名',
      })
      return
    }
    wx.request({
      url: app.globalData.api +'purchasingOrderForm/add',
      data:{
        fabricList: app.globalData.fabricList,
        auxiliaryFabricList: app.globalData.auxiliaryFabricList,
        orderName: this.data.ordName
      },
      header:{
        Authorization: wx.getStorageSync('token')
      },
      method:'POST',
      success:function(res){
        if(res.data.code == 200){
          app.globalData.fabricList = []
          app.globalData.auxiliaryFabricList = []
          that.setData({
            fabricList: app.globalData.fabricList,
            auxiliaryFabricList: app.globalData.auxiliaryFabricList,
            total: 0.00,
            ordName:''
          })
          wx.showToast({
            title: '提交成功',
          })
        }else{
          wx.showToast({
            icon:'none',
            title: res.data.errorMsg,
          })
        }
      }
    })
  },
  //列表
  getMyList: function (status){
    var that = this
    wx: wx.request({
      url: app.globalData.api + 'purchasingOrderForm/myList',
      data: {
        "keyword": "",
        "orderBy": "",
        "status": status,
        "timeRange": "",
        "page": "",
        "rows": 99
      },
      header: {
        Authorization: wx.getStorageSync('token')
      },
      method: 'POST',
      success: function (res) {
        console.log(res)
        if (res.data.code == 200) {
          that.setData({
            myList: res.data.data.purchasingOrderFormList
          })
        }else{
          that.setData({
            myList: []
          })
        }
      }
    })
  },
  getMode:function(e){
    console.log(e)
    this.setData({
      remark: e.currentTarget.dataset.remark,
      callBack: e.currentTarget.dataset.callBack,
      arrears: e.currentTarget.dataset.arrears,
      items:'false',
    })
  },
  noMb:function(){
    this.setData({
      items: 'true',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    var fabricListPrice = 0, auxiliaryFabricListPrice=0
    for (var i in app.globalData.fabricList){
      fabricListPrice += Number(app.globalData.fabricList[i].fabricPrice)
    }
    for (var i in app.globalData.auxiliaryFabricList) {
      auxiliaryFabricListPrice += Number(app.globalData.auxiliaryFabricList[i].auxiliaryFabricPrice)
    }
    this.setData({
      fabricList: app.globalData.fabricList,
      auxiliaryFabricList: app.globalData.auxiliaryFabricList,
      total: (fabricListPrice + auxiliaryFabricListPrice).toFixed(2),
      user: wx.getStorageSync('user')
    })
    this.getMyList(this.data.mycurrent)
  },
  goImg(e) {
    wx.request({
      url: app.globalData.api + 'designDiagram/findDesignDiagramStyleNumber/' + e.currentTarget.dataset.imgid,
      data: '',
      header: {
        Authorization: wx.getStorageSync('token')
      },
      success: function (res) {
        if (res.data.code == 200) {
          wx.navigateTo({
            url: '/pages/details/details?id=' + res.data.data.id,
          })
        } else {
          wx.showModal({
            title: '提示',
            content: res.data.errorMsg,
          })
        }

      }
    })

  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    if (this.data.current == 0) {
      if (this.data.rowsTotal > this.data.list.length) {
        this.getList(this.data.page)
      }
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})